﻿


// Author:					Doan Quoc Tuan
// Created:					2013-5-21
// Last Modified:			2013-5-21
// 
// The use and distribution terms for this software are covered by the 
// Common Public License 1.0 (http://opensource.org/licenses/cpl.php)  
// which can be found in the file CPL.TXT at the root of this distribution.
// By using this software in any fashion, you are agreeing to be bound by 
// the terms of this license.
//
// You must not remove this notice, or any other, from this software.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using MOWebSerivce.Data;

namespace MOWebSerivce.Business
{

    public class SmsMoWaiting
    {

        #region Constructors

        public SmsMoWaiting()
        { }


        public SmsMoWaiting(
            SqlGuid moGuid)
        {
            GetSmsMoWaiting(
                moGuid);
        }

        #endregion

        #region Private Properties

        private SqlGuid _moGuid = SqlGuid.Null;
        private SqlString _subs = SqlString.Null;
        private SqlString _message = SqlString.Null;
        private SqlString _shortCode = SqlString.Null;
        private SqlDateTime _receivedTime = SqlDateTime.Null;
        private SqlString _status = SqlString.Null;
        private SqlInt32 _operatorID = SqlInt32.Null;
        private SqlGuid _smsInGUID = SqlGuid.Null;
        private SqlInt32 _cooperateID = SqlInt32.Null;

        #endregion

        #region Public Properties

        public SqlGuid MoGuid
        {
            get { return _moGuid; }
            set { _moGuid = value; }
        }
        public SqlString Subs
        {
            get { return _subs; }
            set { _subs = value; }
        }
        public SqlString Message
        {
            get { return _message; }
            set { _message = value; }
        }
        public SqlString ShortCode
        {
            get { return _shortCode; }
            set { _shortCode = value; }
        }
        public SqlDateTime ReceivedTime
        {
            get { return _receivedTime; }
            set { _receivedTime = value; }
        }
        public SqlString Status
        {
            get { return _status; }
            set { _status = value; }
        }
        public SqlInt32 OperatorID
        {
            get { return _operatorID; }
            set { _operatorID = value; }
        }
        public SqlGuid SmsInGUID
        {
            get { return _smsInGUID; }
            set { _smsInGUID = value; }
        }
        public SqlInt32 CooperateID
        {
            get { return _cooperateID; }
            set { _cooperateID = value; }
        }

        #endregion

        #region Private Methods

        /// <summary>
        /// Gets an instance of SmsMoWaiting.
        /// </summary>
        /// <param name="moGuid"> moGuid </param>
        private void GetSmsMoWaiting(
            SqlGuid moGuid)
        {
            using (IDataReader reader = DBSmsMoWaiting.GetOne(
                moGuid))
            {
                PopulateFromReader(reader);
            }

        }


        private void PopulateFromReader(IDataReader reader)
        {
            if (reader.Read())
            {
                if (!reader.IsDBNull(0)) this._moGuid = reader.GetGuid(0);
                if (!reader.IsDBNull(1)) this._subs = reader.GetString(1);
                if (!reader.IsDBNull(2)) this._message = reader.GetString(2);
                if (!reader.IsDBNull(3)) this._shortCode = reader.GetString(3);
                if (!reader.IsDBNull(4)) this._receivedTime = reader.GetDateTime(4);
                if (!reader.IsDBNull(5)) this._status = reader.GetString(5);
                if (!reader.IsDBNull(6)) this._operatorID = reader.GetInt32(6);
                if (!reader.IsDBNull(7)) this._smsInGUID = reader.GetGuid(7);
                if (!reader.IsDBNull(8)) this._cooperateID = reader.GetInt32(8);

            }

        }

        /// <summary>
        /// Persists a new instance of SmsMoWaiting. Returns true on success.
        /// </summary>
        /// <returns></returns>
        private bool Create()
        {
            this._moGuid = Guid.NewGuid();

            int rowsAffected = DBSmsMoWaiting.Create(
                this._moGuid,
                this._subs,
                this._message,
                this._shortCode,
                this._receivedTime,
                this._status,
                this._operatorID,
                this._smsInGUID,
                this._cooperateID);

            return (rowsAffected > 0);

        }


        /// <summary>
        /// Updates this instance of SmsMoWaiting. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        private bool Update()
        {

            return DBSmsMoWaiting.Update(
                this._moGuid,
                this._subs,
                this._message,
                this._shortCode,
                this._receivedTime,
                this._status,
                this._operatorID,
                this._smsInGUID,
                this._cooperateID);

        }





        #endregion

        #region Public Methods

        /// <summary>
        /// Saves this instance of SmsMoWaiting. Returns true on success.
        /// </summary>
        /// <returns>bool</returns>
        public bool Save()
        {
            if (this._moGuid != Guid.Empty)
            {
                return Update();
            }
            else
            {
                return Create();
            }
        }




        #endregion

        #region Static Methods

        /// <summary>
        /// Deletes an instance of SmsMoWaiting. Returns true on success.
        /// </summary>
        /// <param name="moGuid"> moGuid </param>
        /// <returns>bool</returns>
        public static bool Delete(
            SqlGuid moGuid)
        {
            return DBSmsMoWaiting.Delete(
                moGuid);
        }


        /// <summary>
        /// Gets a count of SmsMoWaiting. 
        /// </summary>
        public static int GetCount(string sWhereCondition)
        {
            return DBSmsMoWaiting.GetCount(sWhereCondition);
        }

        private static List<SmsMoWaiting> LoadListFromReader(IDataReader reader)
        {
            List<SmsMoWaiting> _smsMoWaitingList = new List<SmsMoWaiting>();
            try
            {
                while (reader.Read())
                {
                    SmsMoWaiting _smsMoWaiting = new SmsMoWaiting();
                    if (!reader.IsDBNull(0)) _smsMoWaiting._moGuid = reader.GetGuid(0);

                    if (!reader.IsDBNull(1)) _smsMoWaiting._subs = reader.GetString(1);

                    if (!reader.IsDBNull(2)) _smsMoWaiting._message = reader.GetString(2);

                    if (!reader.IsDBNull(3)) _smsMoWaiting._shortCode = reader.GetString(3);

                    if (!reader.IsDBNull(4)) _smsMoWaiting._receivedTime = reader.GetDateTime(4);

                    if (!reader.IsDBNull(5)) _smsMoWaiting._status = reader.GetString(5);

                    if (!reader.IsDBNull(6)) _smsMoWaiting._operatorID = reader.GetInt32(6);

                    if (!reader.IsDBNull(7)) _smsMoWaiting._smsInGUID = reader.GetGuid(7);

                    if (!reader.IsDBNull(8)) _smsMoWaiting._cooperateID = reader.GetInt32(8);

                    _smsMoWaitingList.Add(_smsMoWaiting);

                }
            }
            finally
            {
                reader.Close();
            }

            return _smsMoWaitingList;

        }

        /// <summary>
        /// Gets an IList with all instances of SmsMoWaiting.
        /// </summary>
        public static List<SmsMoWaiting> GetAll()
        {
            IDataReader reader = DBSmsMoWaiting.GetAll();
            return LoadListFromReader(reader);

        }

        /// <summary>
        /// Gets an IList with page of instances of SmsMoWaiting.
        /// </summary>
        /// <param name="pageNumber">The page number.</param>
        /// <param name="pageSize">Size of the page.</param>
        /// <param name="totalPages">total pages</param>
        public static List<SmsMoWaiting> GetPage(string sWhereCondition, string sOrderBy, int pageNumber, int pageSize, out int totalPages)
        {
            totalPages = 1;
            IDataReader reader = DBSmsMoWaiting.GetPage(sWhereCondition, sOrderBy, pageNumber, pageSize, out totalPages);
            return LoadListFromReader(reader);
        }



        #endregion

        #region Comparison Methods

        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareBySubs(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.Subs.CompareTo(_smsMoWaiting2.Subs);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByMessage(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.Message.CompareTo(_smsMoWaiting2.Message);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByShortCode(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.ShortCode.CompareTo(_smsMoWaiting2.ShortCode);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByReceivedTime(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.ReceivedTime.CompareTo(_smsMoWaiting2.ReceivedTime);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByStatus(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.Status.CompareTo(_smsMoWaiting2.Status);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByOperatorID(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.OperatorID.CompareTo(_smsMoWaiting2.OperatorID);
        }
        /// <summary>
        /// Compares 2 instances of SmsMoWaiting.
        /// </summary>
        public static int CompareByCooperateID(SmsMoWaiting _smsMoWaiting1, SmsMoWaiting _smsMoWaiting2)
        {
            return _smsMoWaiting1.CooperateID.CompareTo(_smsMoWaiting2.CooperateID);
        }

        #endregion


    }

}





